package com.hy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hy.medicine.evaluation.pojo.MedicineEvaluationReply;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 陌寒    Lenovo
 * @date 2025/3/17上午9:09
 * @description 类描述
 * 评价回复
 */
@Mapper
public interface MedicineEvaluationReplyMapper extends BaseMapper<MedicineEvaluationReply> {
    /**
     * 根据评价ID查询回复列表
     * @param evaluationId 评价ID
     * @return 回复列表
     */
    @Select("SELECT r.*, u.user_name " +
            "FROM medicine_evaluation_reply r " +
            "LEFT JOIN sys_user u ON r.user_id = u.user_id " +
            "WHERE r.evaluation_id = #{evaluationId} " +
            "AND r.reply_status = 1 " +  // 只查询已通过审核的回复
            "ORDER BY r.create_time ASC")
    List<MedicineEvaluationReply> selectRepliesByEvaluationId(@Param("evaluationId") Integer evaluationId);

    /**
     * 根据父回复ID查询子回复列表
     * @param parentId 父回复ID
     * @return 子回复列表
     */
    @Select("SELECT r.*, u.user_name " +
            "FROM medicine_evaluation_reply r " +
            "LEFT JOIN sys_user u ON r.user_id = u.user_id " +
            "WHERE r.parent_id = #{parentId} " +
            "AND r.reply_status = 1 " +
            "ORDER BY r.create_time ASC")
    List<MedicineEvaluationReply> selectChildrenByParentId(@Param("parentId") Integer parentId);

    /**
     * 统计评价的回复数量
     * @param evaluationId 评价ID
     * @return 回复数量
     */
    @Select("SELECT COUNT(*) FROM medicine_evaluation_reply " +
            "WHERE evaluation_id = #{evaluationId} " +
            "AND reply_status = 1")
    int countRepliesByEvaluationId(@Param("evaluationId") Integer evaluationId);

    /**
     * 查询用户的回复列表
     * @param userId 用户ID
     * @return 回复列表
     */
    @Select("SELECT r.*, u.user_name, e.evaluation_content " +
            "FROM medicine_evaluation_reply r " +
            "LEFT JOIN sys_user u ON r.user_id = u.user_id " +
            "LEFT JOIN medicine_evaluation e ON r.evaluation_id = e.evaluation_id " +
            "WHERE r.user_id = #{userId} " +
            "AND r.reply_status = 1 " +
            "ORDER BY r.create_time DESC")
    List<MedicineEvaluationReply> selectUserReplies(@Param("userId") Integer userId);
}
